﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using eCommerce.db.BORespository;
using eCommerce.core.Helpers;
using System.Configuration;
using eCommerce.db;
using System.Web.Configuration;
using eCommerce.core;

namespace eCommerce.web.Controls.Sidebar
{
    public partial class TopView : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                var listFocus = ArticleBo.GetMostView(2);
                if (listFocus != null)
                {
                    rptCategory.DataSource = listFocus;
                    rptCategory.DataBind();
                }
            }
        }

        protected void rptCategory_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                var articleId = Utils.ToInt32(((HiddenField)e.Item.FindControl("hfArticleId")).Value);
                var categoryUrl = ((HiddenField)e.Item.FindControl("hfCategoryUrl")).Value;
                var articleUrl = ((HiddenField)e.Item.FindControl("hfArticleUrl")).Value;
                var imgAvatar = ((HiddenField)e.Item.FindControl("hfImgAvatar")).Value;

                var ltImage = (Literal)e.Item.FindControl("ltImage");
                var imgHref = string.Format(Constant.ThumbImage, WebConfigurationManager.AppSettings["Thumbnail"], imgAvatar, (int)SettingType.Article, 85);

                ltImage.Text = string.Format("<a href=\"{0}\">{1}</a>", UrlHelper.ArticleDetail(categoryUrl, articleId, articleUrl), imgHref);
            }
        }
    }
}